package com.project.controller;

import com.framework.core.convert.impl.CollectionConverter;
import com.framework.core.result.APIListResponse;
import com.framework.core.result.APIObjResponse;
import com.project.base.BaseController;
import com.project.biz.CmsProductCategoryBiz;
import com.project.common.model.UserInfo;
import com.project.entity.CmsProductCategory;
import com.project.pojo.dto.CategoryChildrenDTO;
import com.project.pojo.dto.CategoryDTO;
import com.project.pojo.model.AddOrEditCategoryDTO;
import com.project.pojo.model.CategoryModel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 商品分类远程内部接口
 * @author wangzhiqiang
 * @date 2019年3月6日
 *
 */
@Api(value = "CmsCategoryController", description = "商品类目")
@RestController
@RequestMapping("/category")
@Slf4j
public class CmsCategoryController extends BaseController {

	/**
	 * 商品分类
	 */
    @Autowired
    private CmsProductCategoryBiz categoryBiz;
    
	/**
	 * 获取商品分类
	 * @return
	 */
	@ApiOperation(value = "获取商品类目（前后台通用）", httpMethod = "POST")
	@PostMapping("/getAllCategory")
	public APIListResponse<CategoryDTO> getAllCategory(@RequestBody CategoryModel categoryModel) {
		List<CmsProductCategory> list = this.categoryBiz.getCategory(categoryModel);
		List<CategoryDTO> categoryDTOList = new ArrayList<>();
		if (list.size() > 0){
			CollectionConverter cConverter = new CollectionConverter(List.class, CategoryDTO.class);
			categoryDTOList = (List<CategoryDTO>)cConverter.convert(list, null);
		}

		//存放所有一级
		List<CategoryDTO> categoryDTOs = new ArrayList<>();
		for (CategoryDTO categoryDTO : categoryDTOList){
			if (categoryDTO.getParentId() == 0){
				categoryDTOs.add(categoryDTO);
			}
		}

		for (CategoryDTO categoryDTO : categoryDTOs){
			for (CategoryDTO categoryDTO2 : categoryDTOList) {
				if (categoryDTO.getCategoryId().equals(categoryDTO2.getParentId())){
					CategoryChildrenDTO categoryChildrenDTO = new CategoryChildrenDTO();
					BeanUtils.copyProperties(categoryDTO2, categoryChildrenDTO);
					categoryDTO.getChildrens().add(categoryChildrenDTO);
				}
			}
		}

		return APIListResponse.success("查询成功", categoryDTOs);
	}

	/**
	 * 新增修改商品类目
	 * @param addOrEditCategoryDTO
	 * @return
	 */
	@ApiOperation(value = "后台-新增修改商品类目", httpMethod = "POST")
	@PostMapping("/addOrEditEategory")
	public APIObjResponse<Integer> addOrEditEategory(@RequestBody @Validated AddOrEditCategoryDTO addOrEditCategoryDTO){
		UserInfo userInfo = super.getLoginUser();
		int result = this.categoryBiz.addOrEditEategory(addOrEditCategoryDTO, userInfo.getBaseUserId());
		if (result>0) return APIObjResponse.success("操作成功", result);
		else return APIObjResponse.error("操作失败");
	}
}